# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

#生成训练数据
x = np.linspace(0, 12, 40)
y = np.linspace(0, 5, 40)
#加噪声
y += np.random.randn(40) * 0.5
plt.scatter(x, y)

lrg = LinearRegression()
#转换为二维数组
lrg.fit(x.reshape(-1, 1), y)
#斜率
w_ = lrg.coef_
#截距
b_ = lrg.intercept_

#回归曲线
x1 = np.linspace(0, 12, 300)
y1 = w_ * x1 + b_
plt.plot(x1, y1, color="green", label="slope: %0.2f, intercept: %0.2f" % ( w_, b_))
plt.legend()
plt.show()
